package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo> implements ApartmentInfoService {
    //公寓配套
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    //公寓标签
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    //公寓杂费
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    //公寓图片
    @Autowired
    private GraphInfoService graphInfoService;
    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //判断当前是保存还是更新
        boolean isUpdate = apartmentSubmitVo.getId()!= null && apartmentSubmitVo.getId()!=0;
        //将ApartmentSubmitVo中ApartmentInfo的信息保存或更新到ApartmentInfo表中
        saveOrUpdate(apartmentSubmitVo);

        //获取当前公寓的Id
        Long apartmentId = apartmentSubmitVo.getId();
        //如果是修改功能，则先删除，再添加
        if(isUpdate){
            //删除公寓配套
            LambdaQueryWrapper<ApartmentFacility> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper();
            facilityInfoLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentId);
            apartmentFacilityService.remove(facilityInfoLambdaQueryWrapper);

            //删除公寓标签
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentId);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            //删除公寓杂费
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentId);
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

            //删除图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId,apartmentId);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
        }

        //添加数据
        //添加公寓和配套的关系
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        //通过Batch可以提升性能，减少多次执行SQL语句的性能消耗
        List<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        //不为空则往公寓和配套中间表中添加信息
        if(!CollectionUtils.isEmpty(facilityInfoIds)) {
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentId).facilityId(facilityInfoId).build();
                apartmentFacilities.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }

        //添加公寓和标签的关系
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> apartmentLabels = new ArrayList<>();
        if(!CollectionUtils.isEmpty(labelIds)){
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(apartmentId).labelId(labelId).build();
                apartmentLabels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }

        //添加公寓和杂费的关系
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(apartmentId).feeValueId(feeValueId).build();
                apartmentFeeValues.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }


        //添加公寓和配套的信息

        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfos = new ArrayList<>();
        //如果返回的apartmentSubmitVo.getGraphVoList集合中没有信息，则表示全部被删除了，没有信息，因此不用添加
        if(!CollectionUtils.isEmpty(graphVoList)){
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentId);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }





    }

    //根据条件分页查询公寓列表
    @Override
    public IPage<ApartmentItemVo> findApartmentItemVoPage(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return  apartmentInfoMapper.findApartmentItemVoPage(page,queryVo);
    }

    //根据Id获取公寓详细信息
    @Override
    public ApartmentDetailVo findApartmentDetailVoById(Long apartmentId) {
        //根据id查询公寓信息
        ApartmentInfo apartmentInfo = this.getById(apartmentId);
        //查询公寓所对应的图片信息
        List<GraphVo> graphVoList = graphInfoMapper.findGraphVoListByItemTypeAndItemId(ItemType.APARTMENT,apartmentId);
        //查询公寓对应的配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.findFacilityInfoListByApartmentId(apartmentId);
        //查询公寓对应的标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.findLabelInfoListByApartmentId(apartmentId);
        //查询公寓对应的杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.findFeeValueVoListByApartmentId(apartmentId);
        //创建ApartmentDetailVo对象
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //属性赋值
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;

    }

    //根据id删除公寓信息
    @Override
    public void removeApartmentInfoById(Long apartmentId) {
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId,apartmentId);
        long roomCount = roomInfoService.count(lambdaQueryWrapper);
        //判断当前公寓中是否有房间
        if(roomCount>0){
        //表示公寓中有房间,不可以删除
        throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //表示公寓中没有房间,可以删除

        //删除公寓配套
        LambdaQueryWrapper<ApartmentFacility> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper();
        facilityInfoLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentId);
        apartmentFacilityService.remove(facilityInfoLambdaQueryWrapper);

        //删除公寓标签
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentId);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

        //删除公寓杂费
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentId);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

        //删除图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT).eq(GraphInfo::getItemId,apartmentId);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        //删除当前表中的apartment信息
        this.removeById(apartmentId);

    }
}




